# coding:utf-8
import base64
import datetime
import json
import os
import random
import re
import time
from datetime import date
import uuid
from io import BytesIO

import requests
from flask_mail import Mail
from PIL import Image
from flask import Blueprint, render_template, request, redirect, url_for, flash, session, jsonify, app, make_response, \
    current_app
from flask_mail import Message
from sqlalchemy import or_
from werkzeug.utils import secure_filename

import urllib
from App.ext import db, socketio
from App.validate_code import get_verify_code
from .models import User, Item, ShopCar, Order, Search, Provincial, Reduce, ChatMsg, History, FindGood
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
import qrcode
import cv2
from pyzbar.pyzbar import decode
# 查询商品信息
from App.ShowapiRequest import ShowapiRequest
import json

user = Blueprint('user', __name__)

UPLOAD_FOLDER = 'upload'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'mp3'])

if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)
else:
    pass


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@user.route('/hello/')
def hello():
    return "Hello2"

@user.route('/createAll')
def create():
    db.create_all()
    return 'ok'


# 主页  推荐商品  从search中取出用户搜索记录list
# 使用多条件查询  然后用卖方的信誉度排序
@user.route('/index')
def index():
    user_id = session.get('user_id')
    if not user_id:
        items = Item.query.filter(Item.status == 2).order_by(Item.item_id.desc()).all()[0:8]
        return render_template('index.html', items=items, msg='商品推荐')
    # 通过查询次数推荐
    if user_id:
        # 用户查询次数较高的前三个内容
        search1 = Search.query.filter(Search.user_id == user_id, Search.search_status == 1).order_by(
            Search.search_count.desc()).slice(0, 4).all()
        # 推荐之前没有的商品
        search2 = Search.query.filter(Search.user_id == user_id).filter(Search.search_status == 0).order_by \
            (Search.search_id.desc()).all()
        searchs = search2 + search1
        items = []
        for search in searchs:
            print(search.content)
            items += Item.query.filter(Item.itemName.like('%' + search.content + '%'), Item.status == 2). \
                         all()[0:13]

        # 去除重复的商品
        items2 = []
        for item in items:
            if item not in items2:
                items2.append(item)

        print(items2)
        # 如果用户没有搜索记录
        if not items:
            items = Item.query.filter(Item.status == 2).order_by(Item.item_id.desc()).all()[0:8]
            return render_template('index.html', items=items, user_id=user_id, msg='商品推荐')
        return render_template('index.html', items=items2, user_id=user_id, msg='商品推荐')


# 登录
@user.route('/login/', methods=['post', 'get'])
def login():
    if request.method == 'GET':
        return render_template('login.html')

    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        # 判断用户名和密码是否填写
        if not all([username, password]):
            msg = '* 请填写好完整的信息'
            return render_template('login.html', msg=msg)
        # 核对用户名和密码是否一致
        user = User.query.filter_by(username=username, password=password, user_status=1).first()
        # 如果用户名和密码一致
        if user:
            # 向session中写入相应的数据
            session['user_id'] = user.user_id
            session['username'] = user.username
            session['user_pic'] = user.Icon
            return redirect(url_for('user.index'))
        # 如果用户名和密码不一致返回登录页面,并给提示信息
        else:
            msg = '* 用户名或者密码不一致'
            return render_template('login.html', msg=msg)


# 扫码函数返回扫描信息
def ScanCode():
    msg = ""
    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        msg = "不能打开摄像头！"
        exit(0)

    while True:
        ret, frame = cap.read()
        if not ret:
            msg = "不能获取图像！"
            continue
        results = decode(frame)
        CodeInfo = ''
        for result in results:
            barcode_roi = frame[result.rect.left:result.rect.width, result.rect.top:result.rect.height]
            print(result.data)
            CodeInfo = result.data
        cv2.imshow('camera', frame)
        key = cv2.waitKey(10)
        # 得到条形码的信息
        if CodeInfo:
            msg = "扫码成功"
            break
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()
    return CodeInfo, msg


# 扫码登录
@user.route('/codeLogin', methods=['post', 'get'])
def code_login():
    userInfo, msg = ScanCode()
    # 从二维码中得到用户名得到密码
    userInfo = base64.b64decode(userInfo).decode('utf-8')
    print(userInfo)
    userInfo = json.loads(userInfo)
    print('11111')
    print(userInfo)
    username = userInfo.get('username')
    password = userInfo.get('password')
    print(username)
    print(password)
    # 判断用户名和密码是否填写
    if not all([username, password]):
        msg = '* 缺少用户名和密码！'
        return render_template('login.html', msg=msg)

    # 核对用户名和密码是否一致
    user = User.query.filter_by(username=username, password=password, user_status=1).first()
    # 如果用户名和密码一致
    if user:
        # 向session中写入相应的数据
        session['user_id'] = user.user_id
        session['username'] = user.username
        session['user_pic'] = user.Icon
        return redirect(url_for('user.index'))
    # 如果用户名和密码不一致返回登录页面,并给提示信息
    else:
        msg = '* 用户名或者密码不一致'
        return render_template('login.html', msg=msg)
    return render_template('login.html', msg=msg)


# 注册
@user.route('/register/', methods=['post', 'get'])
def register():
    if request.method == 'GET':
        return render_template("register.html")
    if request.method == 'POST':
        username = request.form.get('username')
        pwd1 = request.form.get('pwd1')
        pwd2 = request.form.get('pwd2')
        email = request.form.get('email')
        code = request.form.get('code')
        image_code = session['image_code']
        phone = request.form.get('phone')
        # 获取短信验证码 获取时间戳
        t = int(time.time())
        timeStamp = session["timeStamp"]
        timeStamp = int(timeStamp)
        Msgcode = session['Msgcode']
        print(t - timeStamp)
        flag = True

        if not all([username, pwd1, pwd2, email, Msgcode, phone]):
            msg, flag = '* 请填写完整信息', False
            return render_template('register.html', msg=msg)
        if t - timeStamp > 80:
            return render_template('register.html', msg="验证码失效！")
        if len(username) > 16:
            msg, flag = '* 用户名太长', False
            return render_template('register.html', msg=msg)
        if pwd1 != pwd2:
            msg, flag = '* 两次密码不一致', False
            return render_template('register.html', msg=msg)
        if code != Msgcode:
            msg, flag = '* 验证码错误', False
            return render_template('register.html', msg=msg)
        if not flag:
            return render_template('register.html', msg=msg)

        u = User.query.filter(User.username == username).first()
        if u:
            msg = '用户名已经存在'
            return render_template('register.html', msg=msg)

        reg_time = time.strftime("%Y-%m-%d", time.localtime())
        # 用户的二维码
        userCode = getUserCode(username, pwd1)
        print(userCode)
        user = User(username=username, password=pwd1, email=email, reg_time=reg_time, user_code=userCode, phone=phone)
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('user.login'))


# 由用户数据得到用户的二维码
def getUserCode(username, password):
    qr = qrcode.QRCode(version=2, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=1)
    userInfo = {"username": username, "password": password}
    userInfo = json.dumps(userInfo).encode()
    # 进行base64加密
    data = base64.b64encode(userInfo)
    qr.add_data(data)
    qr.make(fit=True)
    img = qr.make_image()
    path = 'D:\\PycharmWorkSpace\\bishe0\\static\\userCode\\'
    # 用户名唯一
    filename = str(username) + '.png'
    img.save(path + filename)
    url = '/static/userCode/' + filename
    return url


# 检查用户是否存在
@user.route('/validateUser/')
def validate_user():
    username = request.args.get('username')
    user = User.query.filter(User.username == username).first()
    if user:
        return jsonify({'Code': 200, 'msg': '用户已存在'})
    return jsonify({'Code': 500})


# 获取验证码
@user.route('/code')
def get_code():
    # 返回图片和 验证码
    image, code = get_verify_code()
    # 将验证码图片以二进制形式写入在内存中，防止将图片都放在文件夹中，占用大量磁盘
    buf = BytesIO()
    image.save(buf, 'jpeg')
    buf_str = buf.getvalue()
    # 把二进制作为response发回前端，并设置首部字段
    response = make_response(buf_str)
    response.headers['Content-Type'] = 'image/gif'
    # 将验证码字符串储存在session中
    session['image_code'] = code
    return response  # 返回一个响应


# 退出
@user.route('/logout/', methods=['GET'])
def logout():
    """
    退出登录
    """
    if request.method == 'GET':
        # 清空session
        session['user_id'] = ''
        session['username'] = ''
        # 跳转到登录页面
        return redirect(url_for('user.index'))


# 查看个人信息  修改信息
@user.route('/userInfo/', methods=['GET', 'POST'])
def userInfo():
    if request.method == 'GET':
        username = session.get('username')
        user_id = session.get('user_id')
        if not all([username, user_id]):
            return redirect(url_for('user.login'))
        user = User.query.get(user_id)
        session['password'] = user.password
        return render_template("userInfo.html", user=user)

    if request.method == 'POST':
        username = request.form.get("username")
        phone = request.form.get("phone")
        email = request.form.get("email")
        work = request.form.get("work")
        address1 = request.form.get("address1")
        address2 = request.form.get("address2")
        password = session['password']
        user_id = session['user_id']
        # userCode=getUserCode(username, password)
        user = User.query.get(user_id)
        user.username = username
        user.address1 = address1
        user.address2 = address2
        user.work = work
        user.email = email
        user.phone = phone
        # user.user_code=userCode
        # print(userCode)
        db.session.commit()
        return jsonify({'resultCode': '200'})


# 用户上传图像
@user.route('/uploadFace', methods=['POST'])
def upload_face():
    # request请求中是否包含文件域
    if 'file' not in request.files:
        return jsonify({'resultCode': '500'})
    file = request.files['file']
    if file.filename == '':
        return jsonify({'resultCode': '500'})
    if file and allowed_file(file.filename):
        username = session['username']
        IconName = secure_filename(file.filename)
        filename = username + IconName
        user_id = session['user_id']
        user = User.query.get(user_id)
        url = '/static/face/' + filename
        user.Icon = url
        session['user_pic'] = url
        db.session.commit()
        file.save(os.path.join(UPLOAD_FOLDER, filename))
        pathname = os.path.join(UPLOAD_FOLDER, filename)
        img = Image.open(pathname)
        # 创建缩
        img.thumbnail((128, 128))
        img.save(pathname)
        return jsonify({'resultCode': '200', 'url': url})


# 短信验证码
@user.route('/getMsgCode/', methods=['POST', 'GET'])
def get_MsgCode():
    # 短信验证码
    url = "http://v.juhe.cn/sms/send"
    tel = request.form.get('tel')
    print(tel)
    random_sample = random.sample('1234567890', 6)
    Msgcode = ''
    for i in random_sample:
        Msgcode += i
    session["Msgcode"] = Msgcode
    t = int(time.time())  # 秒级时间戳
    session["timeStamp"] = str(t)
    params = {
        "mobile": tel,  # 接受短信的用户手机号码
        "tpl_id": "160592",  # 您申请的短信模板ID，根据实际情况修改
        "tpl_value": "#code#=" + Msgcode,  # 您设置的模板变量，根据实际情况修改
        "key": "522899acd5978b0acd9ecdbc5f854276",  # 应用APPKEY(应用详细页查询)
    }
    mydata = urllib.parse.urlencode(params).encode('utf-8')
    req = urllib.request.Request(url, mydata)
    data = urllib.request.urlopen(req).read().decode("utf-8")
    jsonData = json.loads(data)
    reason = jsonData.get("reason")
    if reason == "操作成功":
        return jsonify({"Code": 200, "msg": "验证码发送成功！请在一分钟之内填写"})
    else:
        return jsonify({"Code": 500, "msg": "验证码发送失败！"})


# 修改密码
@user.route('/modifyPwd/', methods=['POST', 'GET'])
def modify_pwd():
    if request.method == 'GET':
        return render_template('modifyPwd.html')

    if request.method == 'POST':
        username = request.form.get('username')
        phone = request.form.get('tel')
        pwd1 = request.form.get('password1')
        pwd2 = request.form.get('password2')
        verify_code = request.form.get('code')
        t = int(time.time())
        timeStamp = session["timeStamp"]
        timeStamp = int(timeStamp)
        Msgcode = session['Msgcode']
        print(t - timeStamp)
        user = User.query.filter(User.username == username, User.phone == phone).first()
        if user:
            if t - timeStamp > 60:
                return render_template('modifyPwd.html', msg='*验证码失效')
            if not all([pwd1, pwd2, verify_code]):
                return render_template('modifyPwd.html', msg='*请输入完整信息')
            if pwd1 != pwd2:
                return render_template('modifyPwd.html', msg='*两次密码不一致')
            if pwd1 == pwd2:
                if verify_code != Msgcode:
                    return render_template('modifyPwd.html', msg='*验证码不正确')
                else:
                    user.password = pwd1
                    db.session.commit()
                    # 修改登录码
                    username = user.username
                    getUserCode(username, pwd1)
                    return render_template('modifyPwd.html', msg='修改密码成功')
        else:
            return render_template('modifyPwd.html', msg='请填写正确的信息')

    return render_template('modifyPwd.html')


# 邮箱验证码
@user.route('/sendMailCode/', methods=['POST', "GET"])
def send_mailCode():
    sender = '2277813013@qq.com'
    recipient = request.form.get("recipient")
    random_sample = random.sample('1234567890abcdefghijklmnopqrstuvwxyz', 4)
    code = ''
    for i in random_sample:
        code += i

    session['mailCode'] = code
    msg = "易淘二手交易网站验证码：" + code
    message = MIMEText(msg, 'plain', 'utf-8')
    message['From'] = formataddr(["易淘二手商城", sender])

    subject = '易淘二手交易网站通知'
    message['Subject'] = Header(subject, 'utf-8')

    smtpObj = smtplib.SMTP('smtp.qq.com', port=25)
    smtpObj.login(user=sender, password='dklweoxkokbjeaff')

    smtpObj.sendmail(sender, recipient, message.as_string())
    return jsonify({'Code': '200'})


# 添加商品到购物车
@user.route('/addShopcar/', methods=['GET'])
def add_shopcar():
    item_id = request.args.get('item_id')
    item = Item.query.get(item_id)
    user_id = session['user_id']
    if not user_id:
        return redirect(url_for('user.login'))
    user = User.query.get(user_id)
    if item.count > 0:
        # 当用户购买类似的产品是，修改用户搜索内容
        itemName = item.itemName
        search = Search.query.filter(Search.user_id == user_id, Search.content.like('%' + itemName + '%')).first()
        if search:
            search.search_status = 1
            db.session.commit()

        shopcar = ShopCar.query.filter(ShopCar.item_id == item_id).first()
        if shopcar:
            shopcar.count = shopcar.count + 1
            db.session.commit()
            return jsonify({'Code': 200, 'msg': '商品添加成功'})
        shopcar = ShopCar(pic1=item.pic1, itemName=item.itemName, price=item.price, user_id=user_id, item_id=item_id)
        db.session.add(shopcar)
        db.session.commit()
        return jsonify({'Code': 200, 'msg': '商品添加成功'})
    return jsonify({'Code': 500, 'msg': '商品库存不足'})


# 查看购物车
@user.route('/shopCar/', methods=['GET'])
def shopCar():
    user_id = session['user_id']
    if not user_id:
        return redirect(url_for('user.login'))
    itemList = ShopCar.query.filter_by(user_id=user_id).all()
    if itemList:
        return render_template("shopcar.html", itemList=itemList)

    return render_template("shopcar.html", itemList='')


# 改变购物车中商品数量
@user.route('/changeCount/', methods=['GET', "POST"])
def change_count():
    ShopCar_id = request.form.get('shopCar_id')
    count = request.form.get('count')
    print(count)
    print(ShopCar_id)
    shopCar = ShopCar.query.get(ShopCar_id)
    shopCar.count = count
    db.session.commit()
    return jsonify({'Code': 200})


# 结算购物车
@user.route('/CloseShopCar/', methods=['GET'])
def Close_shopCar():
    # 从购物车中取商品
    ShopCar_id = request.args.get('shopCar_id')
    print(ShopCar_id)
    shopCar = ShopCar.query.get(ShopCar_id)
    item_id = shopCar.item_id
    # 从item表中取正常商品
    item = Item.query.filter(Item.item_id == item_id, Item.status == 2).first()
    if item:
        # 商品库存不足
        if item.count < shopCar.count:
            msg = "商品仅剩" + str(item.count) + "件"
            return jsonify({'Code': 500, 'msg': msg})

        # 商品库存充足
        # 修改item表中库存数量
        item.count = item.count - shopCar.count
        db.session.commit()

        user1_id = session['user_id']
        user2_id = item.user_id  # 卖方id
        user1 = User.query.get(user1_id)
        user2 = User.query.get(user2_id)
        uid = str(uuid.uuid4())
        Order_uuid = ''.join(uid.split('-'))
        orderTime = time.strftime("%Y-%m-%d", time.localtime())
        itemName = item.itemName
        phone = user1.phone

        # 修改搜索记录
        search = Search.query.filter(Search.user_id == user1_id, Search.content.like('%' + itemName + '%'),
                                     Search.search_status == 0).first()
        if search:
            search.search_status = 1
            db.session.commit()

        # 库存足够时  添加订单
        order = Order(pic1=item.pic1, order_uuid=Order_uuid, price=item.price, condition=item.condition,
                      address1=user1.address1, address2=user2.address2, status="已发货", phone=phone,
                      user1_id=user1_id, user2_id=item.user_id, time=orderTime, itemName=itemName)
        db.session.add(order)
        db.session.commit()

        # 从购物车中删除商品
        db.session.delete(shopCar)
        db.session.commit()
        return jsonify({'Code': 200, 'msg': '商品购买成功'})
    return jsonify({'Code': 500, 'msg': '购买失败'})


# 删除购物车中商品
@user.route('/deleteShopCar/<ShopCar_id>', methods=['GET'])
def delete_ShopCar(ShopCar_id):
    shopCar = ShopCar.query.get(ShopCar_id)
    db.session.delete(shopCar)
    db.session.commit()
    return redirect(url_for('user.shopCar'))

# 删除已发布的商品
@user.route('/deleteFindGood/<item_id>', methods=['GET', 'POST'])
def deleteFindGood(item_id):
    item = FindGood.query.get(item_id)
    db.session.delete(item)
    db.session.commit()
    return redirect(url_for('admin.addFindGoodItem'))

# # 上传信息   取出信息  上传图片 创建对象  插入数据库
@user.route('/uploadFindGood/', methods=['GET', 'POST'])
def uploadFindGood():
    if request.method == 'GET':
        return render_template("uploadItem1.html")
    if request.method == 'POST':
        title = request.form.get('title')
        content = request.form.get('content')
        name = request.form.get('name')
        status = request.form.get('status')
        phone = request.form.get('phone')
        # 上传图片
        file1 = request.files.get('file1')
        file2 = request.files.get('file2')
        file3 = request.files.get('file3')
        # urls将文件名添加到数据库

        print([title, content, name, status, phone])
        print([file1, file2, file3])

        if not all([title, content, name, status, phone]):
            return jsonify({'Code': 500, 'msg': '请填写完整信息'})

        pattern = re.compile(r'^[-+]?[-0-9]\d*\.\d*|[-+]?\.?[0-9]\d*$')
        result = pattern.match(phone)
        if not result:
            return jsonify({'Code': 500, 'msg': '请填写正确手机号'})

        urls = []
        if all([file1, file2, file3]):
            # 保存图片
            files = [file1, file2, file3]
            for file in files:
                if file:
                    picname = secure_filename(file.filename)
                    urls.append('/static/itemPic/' + picname)
                    # 上传图片
                    basepath = os.path.dirname(__file__)
                    file_dir = os.path.join(basepath, '../static/itemPic/')
                    file.save(os.path.join(file_dir, picname))
        itemTime = time.strftime("%Y-%m-%d", time.localtime())
        db.create_all()
        print(urls)
        # 存贮到数据库
        item = FindGood(title=title, content=content, name=name, status=status,
                    pic1=urls[0], pic2=urls[1], pic3=urls[2], time=itemTime, phone=phone)

        db.session.add(item)
        db.session.commit()
        return jsonify({'Code': 200, 'pic': urls})

    return jsonify({'Code': 500})

# # 上传商品   取出信息  上传图片 创建对象  插入数据库
@user.route('/uploadItem/', methods=['GET', 'POST'])
def uploadItem():
    if request.method == 'GET':
        return render_template("uploadItem1.html")
    if request.method == 'POST':
        user_id = session['user_id']
        if not user_id:
            redirect(url_for('user.login'))

        user = User.query.get(user_id)
        itemname = request.form.get('itemname')
        price = request.form.get('price')
        config = request.form.get('config')
        detail = request.form.get('detail')
        categary = request.form.get('categary')
        condition = request.form.get('condition')

        Violate = ""
        with open('App/Violate.txt', 'r', encoding='utf-8') as f:
            Violate = f.read()
        Violate=Violate.upper()
        if Violate.find(itemname.upper()) > 0:
            return jsonify({'Code': 500, 'msg': '商品名称中包含违禁信息！'})
        if Violate.find(detail) > 0:
            return jsonify({'Code': 500, 'msg': '商品详情中包含违禁信息！'})

        count = request.form.get('count')
        file11 = request.form.get('file11')
        file22 = request.form.get('file22')
        file33 = request.form.get('file22')
        user_credit = user.credit
        # 上传图片
        file1 = request.files.get('file1')
        file2 = request.files.get('file2')
        file3 = request.files.get('file3')
        # urls将文件名添加到数据库

        print([itemname, price, detail, categary, condition, count])
        print([file1, file2, file3])
        print([file11, file22, file33])

        if not all([itemname, price, detail, categary, condition, count]):
            return jsonify({'Code': 500, 'msg': '请填写完整信息'})

        pattern = re.compile(r'^[-+]?[-0-9]\d*\.\d*|[-+]?\.?[0-9]\d*$')
        result = pattern.match(price)
        result1 = pattern.match(count)
        if not result:
            return jsonify({'Code': 500, 'msg': '请填写正确的价格'})
        if float(price) < 0:
            return jsonify({'Code': 500, 'msg': '商品价格有误'})
        if not result1 or float(count) < 0:
            return jsonify({'Code': 500, 'msg': '商品数量有误！'})
        price = float(price)

        urls = []
        if all([file1, file2, file3]):
            # 保存图片
            files = [file1, file2, file3]
            for file in files:
                if file and allowed_file(file.filename):
                    picname = secure_filename(file.filename)
                    urls.append('/static/itemPic/' + picname)
                    # 上传图片
                    FOLDER = 'D:\\PycharmWorkSpace\\bishe0\\static\\itemPic'
                    file.save(os.path.join(FOLDER, picname))
        else:
            urls = [file11, file22, file33]

        # 保存视频
        video = request.files.get('file4')
        print(video)
        v_url = ''
        if video:
            videoName = secure_filename(video.filename)
            print(videoName)
            VIDEO_FOLDER = 'D:\\PycharmWorkSpace\\bishe0\\static\\video'
            # VIDEO_FOLDER = '/static/video/'
            video.save(os.path.join(VIDEO_FOLDER, videoName))
            v_url = '/static/video/' + videoName
            print(v_url)

        itemTime = time.strftime("%Y-%m-%d", time.localtime())
        print(urls)
        # 存贮到数据库
        item = Item(itemName=itemname, price=price, detail=detail, count=count,
                    categary=categary, status='1', condition=condition,
                    pic1=urls[0], pic2=urls[1], pic3=urls[2], user_id=user_id, time=itemTime,
                    config=config, user_credit=user_credit, video=v_url)

        db.session.add(item)
        db.session.commit()
        return jsonify({'Code': 200, 'pic': urls})

    return jsonify({'Code': 500})


@user.route('/codeUpload1/', methods=['GET', 'POST'])
def code_Upload1():
    img1 = get_ItemPic("http://open.6api.net/lpic/s29713126.jpg")
    img2 = get_ItemPic("http://open.6api.net/lpic/s29713126.jpg")
    return jsonify({"Code": 200, "Categray": "ISBN", "title": "新东方·恋练有词", "summary": "", "price": "58.00",
                    "img1": img1,
                    "img2": img2})


# 扫码上传商品
@user.route('/codeUpload/', methods=['GET', 'POST'])
def code_Upload():
    code, msg = ScanCode()
    code = code.decode('utf-8')
    print(code)
    # IBSN查询
    if code.startswith("9787"):
        url = "http://feedback.api.juhe.cn/ISBN?sub=" + code
        print(type(url))
        print(url)
        params = {
            "key": "c7dc85b3c179ee659a85af47742a46c7",  # 应用APPKEY(应用详细页查询)
        }
        mydata = urllib.parse.urlencode(params).encode('utf-8')
        req = urllib.request.Request(url, mydata)
        data = urllib.request.urlopen(req).read().decode("utf-8")
        # 得到json数据
        jsonData = json.loads(data)
        reason = jsonData.get("reason")
        result = jsonData.get("result")
        subtitle = result.get("subtitle")
        images_medium = result.get("images_medium")
        img1 = get_ItemPic(images_medium)
        images_large = result.get("images_large")
        img2 = get_ItemPic(images_large)
        title = result.get("title")
        summary = result.get("summary")
        price = result.get("price")
        if reason == "查询成功":
            return jsonify({"Code": 200, "Categray": "ISBN", "title": title, "summary": summary, "price": price,
                            "img1": img1, "img2": img1})
        else:
            return jsonify({"Code": 500, "msg": "扫码失败！"})
    else:
        r = ShowapiRequest("http://route.showapi.com/66-22", "96030", "935350c45c104ece85c30ab2068c449b")
        r.addBodyPara("code", code)
        res = r.post()
        jsonData = json.loads(res.text)
        Data = jsonData.get("showapi_res_body")
        print(Data)
        remark = Data.get("remark")
        goodName = Data.get("goodsName")
        manuName = Data.get("manuName")
        goodsType = Data.get("goodsType")
        manuAddress = Data.get("manuAddress")
        trademark = Data.get("trademark")
        spec = Data.get("spec")
        price = Data.get("price")
        img = Data.get("img")
        sptmImg = Data.get("sptmImg")
        if remark == "查询成功！":
            return jsonify(
                {"Code": 200, "Categray": "goods", "goodName": goodName,
                 "manuName": manuName, "price": price, "img": img, "sptmImg": sptmImg})
        else:
            return jsonify({"Code": 500, "msg": "扫码失败！"})


# 得到商品图片
def get_ItemPic(url):
    path = "D:\\PycharmWorkSpace\\bishe0\\static\\itemPic\\"
    t = int(time.time())
    filename = str(t) + ".jpg"
    pic_path = path + filename
    response = requests.get(url)
    f = open(pic_path, 'wb')
    f.write(response.content)
    f.close
    url = "/static/itemPic/" + filename
    return url


# 上传视频
@user.route('/uploadcVideo', methods=['POST', 'GET'])
def uploadc_Video():
    pass


# 上传商品图片
@user.route('/uploadPic', methods=['POST'])
def upload_Pic():
    # request请求中是否包含文件域
    if 'file' not in request.files:
        return jsonify({'resultCode': '500'})
    file = request.files['file']
    if file.filename == '':
        return jsonify({'resultCode': '500'})
    if file and allowed_file(file.filename):
        username = session['username']
        IconName = secure_filename(file.filename)
        filename = username + IconName
        user_id = session['user_id']
        user = User.query.get(user_id)
        url = '/static/face/' + filename
        user.Icon = url
        db.session.commit()
        file.save(os.path.join(UPLOAD_FOLDER, filename))
        pathname = os.path.join(UPLOAD_FOLDER, filename)
        img = Image.open(pathname)
        # 创建缩
        img.thumbnail((128, 128))
        img.save(pathname)
        return jsonify({'resultCode': '200', 'url': url})


# 查看已发布的商品
@user.route('/publishedItem/', methods=['GET', 'POST'])
def publishedItem():
    user_id = session['user_id']
    if not user_id:
        redirect(url_for('user.login'))
    items = Item.query.filter(Item.user_id == user_id).all()
    if not items:
        return render_template("publishedItem.html", items=items, msg='你还没有上传商品')
    return render_template("publishedItem.html", items=items)


# 删除已发布的商品
@user.route('/deleteItem/<item_id>', methods=['GET', 'POST'])
def delete_Item(item_id):
    item = Item.query.get(item_id)
    db.session.delete(item)
    db.session.commit()
    return redirect(url_for('user.publishedItem'))


# 查看已发布商品的详情  修改商品详情
@user.route('/reviseItem/', methods=['GET', 'POST'])
def revise_item():
    if request.method == 'GET':
        item_id = request.args.get('item_id')
        session['item_id'] = item_id
        item = Item.query.get(item_id)
        return render_template("reviseItem.html", item=item)

    if request.method == 'POST':
        itemName = request.form.get('itemName')
        price = request.form.get('price')
        detail = request.form.get('detail')
        condition = request.form.get('condition')
        address1 = request.form.get('address1')
        config = request.form.get('config')
        item_id = session['item_id']

        # 获取商品
        item = Item.query.get(item_id)
        item.itemName = itemName
        oldPrice = item.price
        item.price = price
        item.detail = detail
        item.condition = condition
        item.address1 = address1
        item.config = config

        db.session.commit()

        if float(price) < oldPrice:
            gap = oldPrice - float(price)
            session['gap'] = gap
            session['itemName'] = itemName
        return jsonify({'resultCode': '200'})

    return render_template("reviseItem.html", item='')


# 用邮件通知用户 商品降价
@user.route('/emailNotify/<item_id>', methods=['GET'])
def email_notify(item_id):
    gap = session.get('gap')
    itemName = session['itemName']

    # 在reduce表中获取邮件列表
    reduces = Reduce.query.filter_by(item_id=item_id).all()
    emails = []
    for reduce in reduces:
        emails.append(reduce.email)
    msg = "您关注的" + itemName + "，已经降价" + str(gap) + "元 !"
    sendMail(msg, emails)
    return jsonify({'Code': 200})


def sendMail(msg, emails):
    sender = '2277813013@qq.com'  # 邮件发送者的邮箱地址

    # 三个参数：第一个为邮件正文文本内容，第二个 plain 设置文本格式，第三个 utf-8 设置编码
    message = MIMEText(msg, 'plain', 'utf-8')
    message['From'] = formataddr(["易淘二手商城", sender])  # 发送者

    subject = '易淘二手交易网站通知'
    message['Subject'] = Header(subject, 'utf-8')  # 邮件的主题

    smtpObj = smtplib.SMTP('smtp.qq.com', port=25)
    smtpObj.login(user=sender, password='dklweoxkokbjeaff')  # password并不是邮箱的密码，而是开启邮箱的授权码
    for recipient in emails:
        smtpObj.sendmail(sender, recipient, message.as_string())  # 发送邮件


# 添加商品到订单  user1  买方 user2卖方
@user.route('/addOrder/', methods=['GET'])
def add_Order():
    item_id = request.args.get('item_id')
    item = Item.query.get(item_id)
    if item:
        user1_id = session['user_id']
        user2_id = item.user_id
        user1 = User.query.get(user1_id)
        user2 = User.query.get(user2_id)
        uid = str(uuid.uuid4())
        Order_uuid = ''.join(uid.split('-'))
        orderTime = time.strftime("%Y-%m-%d", time.localtime())
        itemName = item.itemName
        phone = user1.phone

        # 修改搜索记录
        search = Search.query.filter(Search.user_id == user1_id, Search.content.like('%' + itemName + '%')).first()
        if search and search.search_status == 0:
            search.search_status = 1
            db.session.commit()
        # 判断商品库存
        itemCount = item.count
        itemCount = itemCount - 1
        if itemCount <= 0:
            item.status = 0
            db.session.commit()
            return jsonify({'Code': 500, 'msg': '库存不足'})

        item.count = itemCount
        db.session.commit()
        order = Order(pic1=item.pic1, order_uuid=Order_uuid, price=item.price, condition=item.condition,
                      address1=user1.address1, address2=user2.address2, status="已发货", phone=phone,
                      user1_id=user1_id, user2_id=item.user_id, time=orderTime, itemName=itemName)
        db.session.add(order)
        db.session.commit()
        return jsonify({'Code': 200, 'msg': '商品购买成功'})
    return jsonify({'Code': 500, 'msg': '商品购买失败'})


# 添加降价通知   返回商品详情
@user.route('/reduce/', methods=['GET'])
def item_reduce():
    user_id = session['user_id']
    item_id = request.args.get('item_id')
    user = User.query.filter_by(user_id=user_id).first()
    reduce = Reduce.query.filter_by(user_id=user_id, item_id=item_id).first()

    if reduce:
        reduce.email = user.email
        db.session.commit()
        return jsonify({'Code': 200, 'msg': '你已关注该商品'})
    else:
        reduce = Reduce(user_id=user_id, item_id=item_id, email=user.email)
        db.session.add(reduce)
        db.session.commit()
        return jsonify({'Code': 200, 'msg': '关注成功'})


# 查看商品详情
@user.route('/detail/<item_id>', methods=['GET'])
def detail(item_id):
    item = Item.query.get(item_id)
    # 统计商品游览次数
    item.visit_count = item.visit_count + 1
    db.session.commit()
    # 获取卖方信息
    user2_id = item.user_id
    user = User.query.get(user2_id)

    # 买方信息
    user1_id = session.get('user_id')
    if not user1_id:
        return render_template("detail.html", item=item, user=user)
    # 当用登录时可以记录游览历史
    else:
        time = date.today()
        history = History.query.filter_by(item_id=item_id, user_id=user1_id).first()
        if history:
            history.time = time
            db.session.commit()
        else:
            history = History(item_id=item_id, user_id=user1_id,
                              item_pic=item.pic1, item_name=item.itemName, item_categary=item.categary, time=time)
            print(item.pic1)
            db.session.add(history)
            db.session.commit()

        search = Search.query.filter(Search.user_id == user1_id, Search.content.like('%' + item.categary + '%')).first()
        if search:
            print(search)
            search.search_count = search.search_count + 1
            db.session.commit()
        else:
            search = Search(content=item.categary, user_id=user1_id)
            db.session.add(search)
            db.session.commit()
        return render_template("detail.html", item=item, user=user)


# 查看订单
@user.route('/order/', methods=['GET'])
def order():
    user_id = session['user_id']
    page = int(request.args.get('page') or 1)

    OrderList = Order.query.filter_by(user1_id=user_id).paginate(page, 6)
    for i in OrderList.items:
        print(i)
    return render_template("order.html", pagination=OrderList)


# 查看订单详情
@user.route('/orderInfo/<order_id>', methods=['GET'])
def orderInfo(order_id):
    order = Order.query.get(order_id)
    return render_template("orderInfo.html", order=order)


# 删除订单
@user.route('/deleteOrder/<order_id>', methods=['GET'])
def delete_order(order_id):
    order = Order.query.get(order_id)
    db.session.delete(order)
    db.session.commit()
    return redirect(url_for('user.order'))


# 评价订单  获取用户评分  user表中 统计总评分   统计交易次数  计算信誉度
@user.route('/evaluate/', methods=['GET', 'POST'])
def evaluate():
    order_id = request.form.get('order_id')
    start = request.form.get('start')

    order = Order.query.get(order_id)
    order.start = start
    db.session.commit()

    user2_id = order.user2_id
    user = User.query.get(user2_id)
    user.trans_count = user.trans_count + 1
    user.start_cout = user.start_cout + int(start)
    a = (user.start_cout / user.trans_count) * 20
    user.credit = round(a, 2)
    db.session.commit()
    return jsonify({'Code': 200})


# 评价订单
@user.route('/comment/', methods=['GET', 'POST'])
def comment():
    order_id = request.form.get('order_id')
    comment = request.form.get('comment')
    order = Order.query.get(order_id)
    order.comment = comment
    order.status = "已收货"
    db.session.commit()
    return jsonify({'Code': 200})


# 搜索商品  将搜索的内容存到search表中   统计搜索的次数 有相同的搜索内容该次数自增
# 如果无返回内容   做标记
@user.route('/search/', methods=['GET', 'POST'])
def search():
    if request.method == 'POST':
        content = request.form.get('search')

        if not content:
            return redirect(url_for('user.index'))
        user_id = session['user_id']
        items = Item.query.filter(Item.itemName.like('%' + content + '%')).all()

        search = Search.query.filter(Search.user_id == user_id, Search.content.like('%' + content + '%')).first()
        if search:
            search.search_count = search.search_count + 1
            db.session.commit()

        # 没有相同的搜索记录
        else:
            if items:
                search = Search(content=content, user_id=user_id, search_status=1)
                db.session.add(search)
                db.session.commit()
            else:
                search = Search(content=content, user_id=user_id)
                db.session.add(search)
                db.session.commit()
        return render_template("index.html", items=items, msg='搜索结果', user_id=user_id)
    return render_template("index.html", items='', msg='搜索结果', user_id=session['user_id'])


# 查看商品列表
@user.route('/itemList/<categary>', methods=['GET'])
def itemList(categary):
    session['categary'] = categary
    # 默认为综合排序
    session['method'] = ''
    return redirect(url_for('user.paginate'))


# 选择排序
@user.route('/orderMethod/<method>', methods=['GET'])
def order_method(method):
    session['method'] = method
    return redirect(url_for('user.paginate'))


# 分页查询
@user.route('/paginate/', methods=['GET'])
def paginate():
    categary = session['categary']
    method = session.get('method')
    page = int(request.args.get('page') or 1)
    pageObj = ''
    if method == "" or method == '综合排序':
        # 页码page，每页显示10条
        pageObj = Item.query.filter_by(categary=categary, status=2).order_by(Item.user_credit.desc()).paginate(page, 8)
    if method == '游览量':
        pageObj = Item.query.filter_by(categary=categary, status=2).order_by(Item.visit_count.desc()).paginate(page, 8)

    if method == '发布时间':
        pageObj = Item.query.filter_by(categary=categary, status=2).order_by(Item.item_id.desc()).paginate(page, 8)

    return render_template('itemList.html', pagination=pageObj, categary=categary)


# 查看附近
@user.route('/nearby', methods=['GET'])
def nearby():
    username = session.get('username')
    if not username:
        return redirect(url_for('user.login'))
    user = User.query.filter(User.username == username).first()
    user_id = user.user_id
    address = user.address2
    if not address:
        return render_template('itemList.html', items="", categary="附近的商品", msg="请确认当前地址")

    # 获取城市名称
    start = address.find("省")
    city = ""
    if start > 0:
        end = address.find("市")
        city = address[start + 1:end]
    else:
        end = address.find("市")
        city = address[0:end]

    # 模糊查询查找附近的人
    users = User.query.filter(User.address2.like('%' + city + '%')).all()
    items = []
    for user in users:
        if user.user_id != user_id:
            item = Item.query.filter_by(user_id=user.user_id, status=2).all()
            print(item)
            items += item

    print(items)
    categary = '附近的商品'
    if not items:
        return render_template('itemList.html', items=items, categary=categary, msg="你附近没有同城的商品！")
    return render_template('itemList.html', items=items, categary=categary)


@user.route('/test/', methods=['GET'])
def test():
    return render_template("itemList.html")


# 查看已上传物品
@user.route('/uploadInfo/', methods=['GET'])
def uploadInfo():
    return render_template("uploadInfo.html")


@user.route('/page')
def page():
    # 传递的页码数量
    page = int(request.args.get('page') or 1)

    # 页码page，每页显示10条
    pageObj = Item.query.filter_by(categary='a').paginate(page, 5)
    return render_template('goodIndex.html', pagination=pageObj)


# 修改商品信息
@user.route('/reviseItem/', methods=['GET', 'POST'])
def reviseItem():
    return render_template("reviseItem.html")


# 及时通讯
# {'name': '/static/face/abofang.jpg', 'msg': 'ww', 'item_id': 56}
name_space = '/abcd'
msgs = []


# 以用户名决定事件名
# 用户发送消息  其他页面消息提示
@socketio.on('my event', namespace=name_space)
def handle_json(data):
    print('新消息: ' + str(data))
    user_pic = session.get('user_pic')
    user_id = session.get('user_id')
    if not all([user_pic, user_id]):
        return redirect(url_for('user.login'))

    item_id = data['item_id']

    session['item_id'] = item_id
    msg = data['msg']

    # 通过商品id 得到卖方id
    item = Item.query.get(item_id)
    # user1自己  user是对方
    seller_id = item.user_id

    # 将聊天消息查到数据库中
    cur_time = datetime.datetime.now()
    messege = ChatMsg(user1_id=user_id, user_pic=user_pic, msg=msg,
                      item_id=item_id, user2_id=seller_id, time=cur_time)
    db.session.add(messege)
    db.session.commit()

    data = {'name': user_pic, 'msg': msg, 'item_id': item_id, 'seller': seller_id}
    print(data)
    msgs.append(data)
    # jsonData =json.dumps(data)
    print('打印所有消息')
    print(msgs)
    # hint ={'seller':seller_id,'item_id':item_id}
    hint = seller_id
    print(hint)
    socketio.emit('response', data, namespace=name_space, broadcast=True)
    socketio.emit('hint', hint, namespace=name_space, broadcast=True)  # 加入用户id用于区分用户


@user.route('/getMsgCount/', methods=['POST', 'GET'])
def get_msgCount():
    print('get_msgCount')
    user_id = request.args.get('user_id')
    print(user_id)
    count = 0
    for countMsg in msgs:
        print(countMsg)
        print('sssssss' + str(countMsg['seller']))
        if countMsg['seller'] == user_id:
            print(countMsg['seller'])
            count = count + 1
    print(count)
    return jsonify({'code': 200, 'count': count})


# 用js求出消息条数  消息通知   卖方进入聊天界面
# 卖方进入聊天界面
@user.route('/msgHint/', methods=['POST', 'GET'])
def msg_hint():
    user_pic = session.get('user_pic')
    user_id = session.get('user_id')
    # 从数控库中查找出该用户的所有消息
    msgs = ChatMsg.query.filter(or_(ChatMsg.user1_id == user_id, ChatMsg.user2_id == user_id)).all()
    hintList = []
    for msg in msgs:
        item_id = msg.item_id
        if item_id not in hintList:
            hintList.append(msg.item_id)
    # 左侧消息列表(商品ID）
    itemList = []

    for hint in hintList:
        item = Item.query.get(hint)
        itemList.append(item)

    print(itemList)
    # 查询商品
    # item=Item.query.get(item_id)
    # return render_template('userChat.html',item=item)
    # user_pic = session['user_pic']
    # msgList = []
    # for msg in msgs:
    #     if msg['name'] == user_pic:
    #         msgList.append(msg)
    # print(msgList)
    # return 'OK'
    print(hintList)
    # 当前商品
    # 当前用户
    item = Item.query.get(item_id)
    user_id = session.get('user_id')
    user = User.query.get(user_id)
    # 返回当前用户和商品便于判断
    return render_template('chatMsg.html', itemList=itemList, item="", user=user)


# 卖方聊天
@user.route('/sellChat/<item_id>', methods=['POST', 'GET'])
def sellChat(item_id):
    item = Item.query.get(item_id)
    user_id = session.get('user_id')
    user = User.query.get(user_id)
    return render_template("2chat.html", item=item, user=user)


# 加载历史消息
chatMsgs = []


@user.route('/loadMsg/<item_id>', methods=['POST', 'GET'])
def load_msg(item_id):
    if request.method == 'GET':
        jsonData = {'msgs': msgs}
        print("加载历史消息")
        print(jsonData)
        # {'msgs': [{'name': '/static/face/wQQ20190321202911.jpg', 'msg': 'å¥½', 'item_id': 57}]}  jsonData
        # chat_msgs=ChatMsg.query.filter(ChatMsg.item_id==item_id).all()
        # for chat_msg in chat_msgs:
        #     chatItem=

    return jsonify({'Code': 200, 'data': jsonData})


@socketio.on('connect', namespace=name_space)
def connected_msg():
    """socket client event - connected"""
    print('client connected!')


@socketio.on('disconnect', namespace=name_space)
def disconnect_msg():
    """socket client event - disconnected"""
    print('client disconnected!')


# 游览历史列表
@user.route('/historyList')
def historyList():
    user_id = session.get('user_id')
    historys = History.query.filter_by(user_id=user_id)
    return render_template('historyList.html', historys=historys)


# 删除游览历史
@user.route('/delete_history/<h_id>')
def delete_history(h_id):
    history = History.query.get(h_id)
    db.session.delete(history)
    db.session.commit()
    return redirect(url_for('user.historyList'))


# 省市联动
@user.route('/provincial')
def get_provinve():
    provinces = Provincial.query.filter().all()
    p = []
    for province in provinces:
        p.append(province.name)

    print(p)
    return 'ok'
